package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.StoryCard;
import reduce.fz.com.reduce.bean.StoryPicCard;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;
import reduce.fz.com.reduce.custom.NetworkImageView;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.http.ImageLoader;

/**
 * Created by qingshengzheng on 15/10/18.
 */
public class StoryDetailAdapter extends HeaderFooterStatusRecyclerViewAdapter<BaseRecycleViewHolder> {
    //故事详情列表
    private List<StoryPicCard> storyPicCardList=new ArrayList<StoryPicCard>();
    //故事卡片item
    private StoryCard storyCard;

    private Context context;
    private LayoutInflater layoutInflater;

    public StoryDetailAdapter(Context context) {
        this.context = context;
        this.layoutInflater=LayoutInflater.from(context);
    }

    public void initData(StoryCard card) {
        this.storyCard = card;
        this.storyPicCardList = storyCard.picCards;
        notifyDataSetChanged();
    }


    @Override
    public BaseRecycleViewHolder createFooterStatusViewHolder(View footerView) {
        return BaseRecycleViewHolder.getFooter(footerView);
    }

    @Override
    protected int getHeaderItemCount() {
        return 1;
    }

    @Override
    protected int getContentItemCount() {
        return storyPicCardList.size();
    }

    @Override
    protected BaseRecycleViewHolder onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        View view = layoutInflater.inflate(R.layout.story_detail_header_view, parent, false);
        return new DetailHeaderViewHolder(view);

    }

    @Override
    protected BaseRecycleViewHolder onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View view = layoutInflater.inflate(R.layout.story_detail_content_view, parent, false);
        return new DetailContentViewHolder(view);

    }

    @Override
    protected void onBindHeaderItemViewHolder(BaseRecycleViewHolder headerViewHolder, int position) {
        DetailHeaderViewHolder holder = (DetailHeaderViewHolder) headerViewHolder;
        holder.renderView(storyCard, position);

    }

    @Override
    protected void onBindContentItemViewHolder(BaseRecycleViewHolder contentViewHolder, int position) {
        DetailContentViewHolder holder = (DetailContentViewHolder) contentViewHolder;
        holder.renderView(storyPicCardList.get(position), position);

    }

    public class DetailContentViewHolder extends BaseRecycleViewHolder<StoryPicCard> {
        private TextView mTvPicDesc;
        private NetworkImageView mIvStoryPic;

        public DetailContentViewHolder(View itemView) {
            super(itemView);
            mTvPicDesc = (TextView) itemView.findViewById(R.id.tv_pic_desc);
            mIvStoryPic = (NetworkImageView) itemView.findViewById(R.id.iv_story_pic);
        }

        @Override
        public void renderView(StoryPicCard data, int position) {
            mTvPicDesc.setText(data.desc);
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(data.cardurl.width,data.cardurl.height);
            mIvStoryPic.setLayoutParams(params);
            mIvStoryPic.setImageUrl(data.cardurl.url, HttpUtils.getImageLoader());
        }
    }

    public class DetailHeaderViewHolder extends BaseRecycleViewHolder<StoryCard> {
        //标题
        private TextView mTvTitle;
        //发布时间
        private TextView mTvPubTime;
        //作者昵称
        private TextView mTvNickname;
        //阅读次数
        private TextView mTvCountReads;

        public DetailHeaderViewHolder(View itemView) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            mTvPubTime = (TextView) itemView.findViewById(R.id.tv_pub_time);
            mTvNickname = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mTvCountReads = (TextView) itemView.findViewById(R.id.tv_count_reads);
        }

        @Override
        public void renderView(StoryCard data, int position) {
            if(data==null) return;
            mTvTitle.setText(data.title);
            mTvPubTime.setText(DateUtil.timeStamp2Date(data.pubtime,DateUtil.FORMAT_SHORT_CN));
            mTvNickname.setText(data.user==null?null:data.user.nickname);
            mTvCountReads.setText("阅读次数:"+data.storyAttach.count_reads + "");
        }
    }
}
